#ifndef IO_WORHER_H
#define IO_WORHER_H

#include <iostream>
#include <string>
#include <random>

#include "Common.h"
#include "GameState.h"

using namespace std;

// ГПСЧ
mt19937 SYSTEM_RANDOM_GENERATOR;

class IOWorker
{
private:
    static mt19937 randomGenerator;
public:

	static void printConf(const GameConf & cnf)
	{
		 cerr << "Conf = " << endl;
		 cerr << '{' << endl;
		 cerr << "\tisGameOver = " << cnf.isGameOver << endl;
		 cerr << "\tloadtime = " << cnf.loadtime << endl;
		 cerr << "\tturntime = " << cnf.turntime <<endl;
		 cerr << "\trows = " << cnf.rows <<endl;
		 cerr << "\tcols = " << cnf.cols <<endl;
		 cerr << "\tturns = " << cnf.turns <<endl;
		 cerr << "\tviewradius2 = " << cnf.viewradius2 <<endl;
		 cerr << "\tattackradius2 = " << cnf.attackradius2 <<endl;
		 cerr << "\tspawnradius2 = " << cnf.spawnradius2 <<endl;
		 cerr << "\tplayer_seed = " << cnf.player_seed <<endl;
         cerr << "\tdefaultMoves = {" << cnf.defaultMoves[0] << ',' << cnf.defaultMoves[1] << '}' << endl;
		 cerr << '}' << endl << flush;
	}
	static void loadConfTurn(GameConf & cnf)
	{
		string line, buffer;
		int32_t turnNo;
		cnf.isGameOver = false;
        
        // парсинг конфигурации
		while (cin >> line)
		{
			if (line == "turn")
			{
				cin >> turnNo;
				if (turnNo != 0)
				{
					// какая то фигня
				}
			}
			else if (line == "loadtime")
			{
				cin >> cnf.loadtime;
			}
			else if (line == "turntime")
			{
				cin >> cnf.turntime;
			}
			else if (line == "rows")
			{
				cin >> cnf.rows;
			}
			else if (line == "cols")
			{
				cin >> cnf.cols;
			}
			else if (line == "turns")
			{
				cin >> cnf.turns;
			}
			else if (line == "viewradius2")
			{
				cin >> cnf.viewradius2;
			}
			else if (line == "spawnradius2")
			{
				cin >> cnf.spawnradius2;
			}
			else if (line == "player_seed")
			{
				cin >> cnf.player_seed;
			}
			else if (line == "attackradius2")
			{
				cin >> cnf.attackradius2;
			}
			else if (line == "ready")
			{
				break;
			}
			else if (line == "end")
			{
				// конец игры
				cnf.isGameOver = true;
			}
		}
        
        // инициализируем ГПСЧ
        SYSTEM_RANDOM_GENERATOR.seed(cnf.player_seed);
        
        const static char dMoves[4][2] = {{'n','e'}, {'n', 'w'}, {'s','e'}, {'s', 'w'}};
        int32_t movesIndex = IOWorker::getRandomInt(0, 3);
        cnf.defaultMoves[0] = dMoves[movesIndex][0];
        cnf.defaultMoves[1] = dMoves[movesIndex][1];
	}

    static int32_t getRandomInt(int32_t min, int32_t max)
    {
        uniform_int_distribution<int32_t> distribution(min, max);
        return distribution(SYSTEM_RANDOM_GENERATOR);
    }
    
	static void startTurn(GameState & st, GameConf & cnf)
	{
		string word;
		int32_t row, column, player, turnNo;
		while (cin >> word)
		{
            if (word == "a")
			{
				cin >> row >> column >> player;
                Ant tmpAnt(row, column, (player == 0));
				st.setXY(tmpAnt);
			}
			if (word == "h")
			{
				cin >> row >> column >> player;
				st.setXY(row, column, ANTHILL, (player == 0));
			}
			else if (word == "w")
			{
				cin >> row >> column;
				st.setXY(row, column, WATER);
			}
			else if (word == "f")
			{
				cin >> row >> column;
				st.setXY(row, column, FOOD);
			}
			else if (word == "d")
			{
				// мёртвых муравьёв мы не считаем
			}
			else if (word == "turn")
			{
				cin >> turnNo;
			}
			else if (word == "go")
			{
				// конец ввода информации
				break;
			}
			else if (word == "end")
			{
				// конец игры
				cnf.isGameOver = true;
			}
		}
	}

	static void outputTurn(GameState & state)
	{
		for (auto it : state.filteredMovies)
		{
			cout << it << endl;
            //cerr << it << endl;
		}
		cout << flush;
	}

	static void endTurn()
	{
        //cerr << "go" << endl << flush;
		cout << "go" << endl << flush;
	}
	
	static void endTurn(GameState & st)
	{
		st.cleanAfterTurn();
		IOWorker::endTurn();
	}
	
	
};

#endif /* IO_WORHER_H */
